package com.hmily.dubbo.consumer.sync;

import com.hmily.dubbo.api.UserQueryFacade;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.MethodConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.RpcContext;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <h1>利用 java 代码来启动消费方。</h1>
 *
 * @author hmilyylimh
 * ^_^
 * @version 0.0.1
 * ^_^
 * @date 2021-10-18
 */
public class LookRpcContextConsumerApplication {

    public static void main(String[] args) throws IOException {
        // 设置应用服务名称
        ApplicationConfig applicationConfig = new ApplicationConfig("dubbo-consumer");
        // 设置注册中心的地址
        RegistryConfig registryConfig = new RegistryConfig("zookeeper://127.0.0.1:2181");

        // 创建引用 UserQueryFacade 这个服务的对象
        ReferenceConfig<UserQueryFacade> referenceConfig = new ReferenceConfig<>();
        referenceConfig.setApplication(applicationConfig);
        referenceConfig.setRegistry(registryConfig);
        referenceConfig.setInterface(UserQueryFacade.class);
        referenceConfig.setLoadbalance("roundrobin");
        // referenceConfig.setCache("lru");

        // 配置到方法级别
        List<MethodConfig> methods = new ArrayList<>(1);
        MethodConfig methodConfig = new MethodConfig();
        methodConfig.setName("queryUser");
        methodConfig.setCache("lru");
        methods.add(methodConfig);
        referenceConfig.setMethods(methods);


        UserQueryFacade userQueryFacade = referenceConfig.get();

        RpcContext context = RpcContext.getContext();

        // TODO 注意带大家看看 RpcContext 里面都有一些什么东西

        // 阻塞等待
        System.in.read();
    }
}
